package com.jdy.haoduoaiteacher.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;

import com.jdy.haoduoaiteacher.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

@SuppressLint("AppCompatCustomView")
public class RoundImageView extends ImageView {

  /**
   * 图片的类型，圆形or圆角
   */
  private final int type;
  private static final int TYPE_CIRCLE = 0;
  private static final int TYPE_ROUND = 1;

  /**
   * 圆角大小的默认值
   */
  private static final int BODER_RADIUS_DEFAULT = 10;
  /**
   * 圆角的大小
   */
  private final int mBorderRadius;

  /**
   * 绘图的Paint
   */
  private final Paint mBitmapPaint;

  private Paint paintOut;

  /**
   * 圆角的半径
   */
  private int mRadius;
  /**
   * 3x3 矩阵，主要用于缩小放大
   */
  private final Matrix mMatrix;
  /**
   * 渲染图像，使用图像为绘制图形着色
   */
  private BitmapShader mBitmapShader;
  /**
   * view的宽度
   */
  private int mWidth;
  private RectF mRoundRect;

  public RoundImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mMatrix = new Matrix();
    mBitmapPaint = new Paint();
    mBitmapPaint.setAntiAlias(true);
    paintOut = new Paint();
    paintOut.setAntiAlias(true);
    paintOut.setColor(Color.YELLOW); // 这里的颜色决定了边缘的颜色

    TypedArray a = context.obtainStyledAttributes(attrs,
        R.styleable.RoundImageView);

    mBorderRadius = a.getDimensionPixelSize(
        R.styleable.RoundImageView_borderRadius, (int) TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, BODER_RADIUS_DEFAULT,
                getResources().getDisplayMetrics()));// 默认为10dp
    type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为Circle

    a.recycle();


    cm.setSaturation(0.2f);
    mColorMatrixColorFilter = new ColorMatrixColorFilter(cm);
  }

  ColorMatrixColorFilter mColorMatrixColorFilter;

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Log.e("TAG", "onMeasure");
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    /**
     * 如果类型是圆形，则强制改变view的宽高一致，以小值为准
     */
    if (type == TYPE_CIRCLE) {
      mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
      mRadius = mWidth / 2;
      setMeasuredDimension(mWidth, mWidth);
    }
  }

  /**
   * 初始化BitmapShader
   */
  private void setUpShader() {
    Drawable drawable = getDrawable();
    if (drawable == null) {
      return;
    }

    Bitmap bmp = Tools.drawableToBitamp(drawable);
    // 将bmp作为着色器，就是在指定区域内绘制bmp
    mBitmapShader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP);
    float scale = 1.0f;
    if (type == TYPE_CIRCLE) {
      // 拿到bitmap宽或高的小值
      int bSize = Math.min(bmp.getWidth(), bmp.getHeight());
      scale = mWidth * 1.0f / bSize;

    } else if (type == TYPE_ROUND) {
      // 如果图片的宽或者高与view的宽高不匹配，计算出需要缩放的比例；缩放后的图片的宽高，一定要大于我们view的宽高；所以我们这里取大值；
      scale = Math.max(getWidth() * 1.0f / bmp.getWidth(), getHeight() * 1.0f
          / bmp.getHeight());
    }
    // shader的变换矩阵，我们这里主要用于放大或者缩小
    mMatrix.setScale(scale, scale);
    // 设置变换矩阵
    mBitmapShader.setLocalMatrix(mMatrix);
    // 设置shader
    mBitmapPaint.setShader(mBitmapShader);
  }

  private boolean isSelected;

  @Override
  public boolean isSelected() {
    return isSelected;
  }

  @Override
  public void setSelected(boolean isSelected) {
    this.isSelected = isSelected;
  }

  ColorMatrix cm = new ColorMatrix();

  public void setDrawGray(boolean bDrawGray) {
    this.bDrawGray = bDrawGray;
  }

  public boolean bDrawGray = false;

  @Override
  protected void onDraw(Canvas canvas) {
    if (getDrawable() == null) {
      return;
    }
    setUpShader();

    if (type == TYPE_ROUND) {
      canvas.drawRoundRect(mRoundRect, mBorderRadius, mBorderRadius,
          mBitmapPaint);
    } else {

      if (bDrawGray) {
        mBitmapPaint.setColorFilter(mColorMatrixColorFilter);
      } else {
        mBitmapPaint.setColorFilter(null);
      }

      canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint);

      if (isSelected) {
        paintOut.setStyle(Paint.Style.STROKE); // 绘
        paintOut.setStrokeWidth(4);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2,
            paintOut);
      }

      // drawSomeThing(canvas);
    }
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    // 圆角图片的范围
    if (type == TYPE_ROUND) {
      mRoundRect = new RectF(0, 0, getWidth(), getHeight());
    }
  }

}
